<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
	"http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<script type="text/javascript" src="extjs/ext-all.js"></script>
		<script src="background.js"></script>
		<script>
			var _gaq = _gaq || [];
				_gaq.push(['_setAccount', 'UA-30213088-1']);
				_gaq.push(['_trackPageview']);
			
			(function() {
				var ga = document.createElement('script');
				ga.type = 'text/javascript';
				ga.async = true;
				ga.src = 'https://ssl.google-analytics.com/ga.js';
				var s = document.getElementsByTagName('script')[0];
				s.parentNode.insertBefore(ga, s);
			})();
		</script>
	</head>
	<body>
		<script type="text/javascript">
			function time(seconds) {
				var h = Math.floor(seconds / 3600);
				var m = Math.floor(seconds % 3600 / 60);
				var s = Math.floor(seconds % 3600 % 60);
				return ((h > 0 ? h + ":" : "") + (m > 0 ? (h > 0 && m < 10 ? "0" : "") + m + ":" : "0:") + (s < 10 ? "0" : "") + s);
			}

			bg = {
				audio: new Audio(),
				playlist: null,
				get popup() {
					var x = chrome.extension.getViews({type:"popup"});
					return (x.length > 0)?x[0]:null;
				},
				findInPlaylist:function(track) {
					if (!track && this.currentTrack) track = this.currentTrack;
					var index;
					for (var i = 0; i < this.playlist.length; i++) {
						if (track.get("owner_id") == this.playlist[i].get("owner_id") &&
							track.get("aid") == this.playlist[i].get("aid")) return i;
					}
					return -1;
				},
				get volume() {
					return this.audio.volume * 100.0;
				},
				set volume(value) {
					this.audio.volume = value / 100;
				},
				get currentTrackName() {
					return this.currentTrack?this.currentTrack.get("artist") + " - " + this.currentTrack.get("title"):"Ничего не играет";
				},
				get duration() {
					return this.audio.duration;
				},
				get position() {
					return this.duration?this.time / this.duration * 100.0:0;
				},
				set position(percentage) {
					this.time = this.duration * percentage / 100.0;
				},
				get time() {
					return this.audio.currentTime;
				},
				set time(seconds) {
					this.audio.currentTime = seconds;
				},
				get repeat() {
					return this.audio.loop;
				},
				set repeat(value) {
					this.audio.loop = value;
				},
				togglerepeat:function() {
					this.repeat = !this.repeat; 
				},
				forward:function() {
					var newPosition = Math.min(99, this.position + 10);
					this.position = newPosition;
				},
				rewind:function() {
					var newPosition = Math.max(0, this.position - 10);
					this.position = newPosition;
				},
				volumeup:function() {
					var newVolume = Math.min(100, this.volume + 5);
					this.volume = newVolume;
				},
				volumedown:function() {
					var newVolume = Math.max(0, this.volume - 5);
					this.volume = newVolume;
				},
				togglemute:function() {
					if (localStorage.mute) {
						localStorage.removeItem('mute');
						this.volume = localStorage.lastVolume || 100;
					} else {
						localStorage.mute = 1;
						localStorage.lastVolume = this.volume;
						this.volume = 0;
					}
				},
				play:function() {
					this.audio.play();
					chrome.extension.sendRequest(playpauseId, false);
					_gaq.push(['_trackEvent', 'Controls', 'Play']);
				},
				pause:function() {
					this.audio.pause();
					chrome.extension.sendRequest(playpauseId, true);
					_gaq.push(['_trackEvent', 'Controls', 'Pause']);
				},
				playpause:function() {
					if (this.audio.paused) this.play();
					else this.pause();
					return this.audio.paused;
				},
				next:function(noPopup) {
					var index = this.findInPlaylist();
					if (this.playlist && index < this.playlist.length - 1) {
						this.currentTrack = this.playlist[++index];
						this.playTrack(null, noPopup);
						_gaq.push(['_trackEvent', 'Controls', 'Next']);
					}
				},
				prev:function(noPopup) {
					var index = this.findInPlaylist();
					if (this.playlist && index > 0) {
						this.currentTrack = this.playlist[--index];
						this.playTrack(null, noPopup);
						_gaq.push(['_trackEvent', 'Controls', 'Previous']);
					}
				},
				onTimeupdate:function() {
					chrome.extension.getViews({type:"notification"}).forEach(function(win) {
						win.needReact = false;
						win.Ext.getCmp("position").setValue(bg.time / bg.duration * 100.0);
						win.needReact = true;
					});
					if (this.popup) {
						this.popup.needReact = false;
						this.popup.Ext.getCmp("position").setValue(this.time / this.duration * 100.0);
						this.popup.needReact = true;
					}
					chrome.browserAction.setBadgeText({
						text: time(this.time)
					});
				},
				onVolumechange:function() {
					chrome.extension.getViews({type:"notification"}).forEach(function(win) {
						win.needReact = false;
						win.Ext.getCmp("volume").setValue(bg.audio.volume * 100.0);
						win.needReact = true;
					});
					if (this.popup) {
						this.popup.needReact = false;
						this.popup.Ext.getCmp("volume").setValue(this.audio.volume * 100.0);
						this.popup.needReact = true;
					}
					localStorage["volume"] = this.volume;
				},
				onFinish:function() {
					this.next();
					_gaq.push(['_trackEvent', 'Track', 'Finished']);
				},
				get prevTrack() {
					if (!this.currentTrack || !this.playlist) return null;
					var index = this.findInPlaylist();
					return (index == 0)?null:this.playlist[index - 1];
				},
				get nextTrack() {
					if (!this.currentTrack || !this.playlist) return null;
					var index = this.findInPlaylist();
					return (index == this.playlist.length - 1)?null:this.playlist[index + 1];
				},
				updateControls:function() {
					chrome.extension.getViews({type:"notification"}).forEach(function(win) {
						win.needReact = false;
						win.Ext.getCmp("play").toggle(!bg.audio.paused);
						win.Ext.getCmp("prev").setDisabled(!bg.prevTrack);
						win.Ext.getCmp("next").setDisabled(!bg.nextTrack);
						win.Ext.getCmp("title").setText(bg.currentTrackName);
						win.needReact = true;
					});
					if (this.popup && this.popup.needReact) {
						this.popup.needReact = false;

						this.popup.Ext.getCmp("play").toggle(!this.audio.paused);
						this.popup.Ext.getCmp("play").setDisabled(!this.currentTrack);
						this.popup.Ext.getCmp("repeat").setDisabled(!this.currentTrack);
						this.popup.Ext.getCmp("position").setDisabled(!this.currentTrack);
						this.popup.Ext.getCmp("prev").setDisabled(!this.prevTrack);
						this.popup.Ext.getCmp("next").setDisabled(!this.nextTrack);

						try {
							if (this.popup.playlist) this.popup.playlist.setTitle(this.currentTrackName);
							chrome.browserAction.setTitle({title: this.currentTrackName});
						} catch(e) {}
						if (this.currentTrack) {
							this.popup.playlist.getSelectionModel().select(this.currentTrack);
						}
						this.popup.needReact = true;
					}
					//this.popup.playlist.update();
				},
				playTrack:function(record, noPopup) {
					if (this.currentTrack) this.currentTrack.current = false;
					record.current = true;
					chrome.extension.getViews({type:"notification"}).forEach(function(win) {
						win.needReact = false;
						win.Ext.getCmp("position").setValue(0);
						win.needReact = true;
					});
					if (this.popup) {
						this.popup.needReact = false;
						this.popup.Ext.getCmp("position").setValue(0);
						this.popup.needReact = true;
					}
					if (record) this.currentTrack = record;
					if (record.unfined) this.next();
					this.audio.pause();
					this.audio.src = this.currentTrack.get("url");
					this.audio.play();
					this.updateControls();
					if (!noPopup && localStorage["notify"]) {
						chrome.extension.getViews({type:"notification"}).forEach(function(win) {
							win.close();
						});
						webkitNotifications.createHTMLNotification('notification.html').show();
					}
					_gaq.push(['_trackEvent', 'Track', 'Played']);
					chrome.browserAction.setBadgeText({
						text: "0:00"
					});
				},
				playUrl:function(url) {
					this.audio.pause();
					this.audio.src = url;
					this.audio.play();
				}
			}
			bg.volume = localStorage.volume || 100;
			bg.audio.setAttribute("onended", "bg.onFinish()");
			bg.audio.setAttribute("ontimeupdate", "bg.onTimeupdate()");
			bg.audio.setAttribute("onvolumechange", "bg.onVolumechange()");
		</script>
	</body>
</html>
